#include "spritebox.h"

spriteBox::spriteBox(int x,int y,int imagewidth,int imageheight,int psectionwidth,int psectionheight,const char *filename,bool pautoanim) : Fl_Button(x,y,psectionwidth,psectionheight)
{
    resim=new Fl_PNG_Image(filename);
    sectionwidth=psectionwidth;
    sectionheight=psectionheight;
    row=imageheight/sectionheight;
    column=imagewidth/sectionwidth;
    autoanim=pautoanim;
    animtime=0.10;
    box(FL_FLAT_BOX);
    cx=0;
    cy=0;
    if(autoanim)
        Fl::add_timeout(animtime,anim_CB,this);
}
spriteBox::spriteBox(int x,int y,int w,int h,const char *filename) :Fl_Button(x,y,32,32)
{
    resim=new Fl_PNG_Image(filename);
    sectionwidth=32;
    sectionheight=32;
    row=h/sectionheight;
    column=w/sectionwidth;
    autoanim=false;
    box(FL_BORDER_BOX);
    animtime=0.10;
    cx=0;
    cy=0;
    if(autoanim)
        Fl::add_timeout(animtime,anim_CB,this);
}
spriteBox::spriteBox(int x,int y,int w,int h) : Fl_Button(x,y,32,32)
{
    sectionwidth=32;
    sectionheight=32;
    row=h/sectionheight;
    column=w/sectionwidth;
    autoanim=false;
    box(FL_BORDER_BOX);
    animtime=0.10;
    cx=0;
    cy=0;
    if(autoanim)
        Fl::add_timeout(animtime,anim_CB,this);
}
spriteBox::~spriteBox()
{
    delete resim;
}

void spriteBox::draw()
{
    Fl_Button::draw();
    resim->draw(x(),y(),sectionwidth,sectionheight,cx,cy);
}

void spriteBox::section(int psectionwidth,int psectionheight)
{
    sectionwidth=psectionwidth;
    sectionheight=psectionheight;
}

void spriteBox::anim_CB(void *userdata)
{
    spriteBox *gecici=(spriteBox *)userdata;
    gecici->anim_CB2();
    if(gecici->animationstate())
        Fl::repeat_timeout(gecici->gettime(),gecici->anim_CB,gecici);
}
void spriteBox::anim_CB2()
{
    if(cx<(column-1)*sectionwidth)
    {
        cx+=sectionwidth;
    }
    else
    {
        cx=0;
        if(cy<(row-1)*sectionheight)
        {
            cy+=sectionheight;
        }
        else
        {
            cy=0;
        }
    }
    this->redraw();
}
void spriteBox::settimer(double panimtime)
{
    animtime=panimtime;
}

void spriteBox::setrow(int prow)
{
    row=prow;
}
void spriteBox::setcolumn(int pcolumn)
{
    column=pcolumn;
}

void spriteBox::setsection(int psectionwidth,int psectionheight)
{
    sectionwidth=psectionwidth;
    sectionheight=psectionheight;
}
double spriteBox::gettime()
{
    return animtime;
}
void spriteBox::animationstate(bool panim)
{
    autoanim=panim;
}
bool spriteBox::animationstate()
{
    return autoanim;
}
void spriteBox::spriteposition(int pcx,int pcy)
{
    cx=pcx;
    cy=pcy;
}
